package dao.hibernate;

import java.util.List;

import model.BacSi;
import model.Benh;
import model.ChuDe;
import model.ChuDeKhac;
import model.DiaDiemYTe;
import model.ThietBiYTe;
import model.Thuoc;
import dao.ChuDeDAO;

public class ChuDeDAOHibernate extends GenericHibernateDAO<ChuDe, Long>
		implements ChuDeDAO {
	@SuppressWarnings("unchecked")
	@Override
	public List<ChuDe> findByNameLike(String name) {
		return getSession()
				.createQuery(
						"FROM ChuDe AS chuDe WHERE chuDe.tenChuDe LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChuDe> findAllMainTopic() {
		return getSession().createQuery(
				"FROM ChuDe AS chuDe WHERE chuDe.chuDeCha = null").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChuDe> findAllByChuDe(Object c) {
		long id = 0;
		if (c == Benh.class)
			id = 1;
		else if (c == Thuoc.class)
			id = 2;
		else if (c == DiaDiemYTe.class)
			id = 3;
		else if (c == BacSi.class)
			id = 4;
		else if (c == ThietBiYTe.class)
			id = 5;
		else if (c == ChuDeKhac.class)
			id = 6;

		return getSession()
				.createQuery(
						"FROM ChuDe AS chuDe WHERE chuDe.chuDeCha.idChuDe = :pid")
				.setLong("pid", id).list();
	}

	@Override
	public ChuDe findByName(String name) {
		return (ChuDe) getSession()
				.createQuery(
						"FROM ChuDe AS chuDe WHERE chuDe.tenChuDe = :pname")
				.setString("pname", name).uniqueResult();
	}

	@Override
	public ChuDe findChuDeChaByLoai(String loai) {
		return (ChuDe) getSession()
				.createQuery(
						"FROM ChuDe AS chuDe WHERE chuDe.tenChuDe = :pname")
				.setString("pname", loai).uniqueResult();

	}
}
